package com.lovestudying.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lovestudying.service.ManagerService;
import com.lovestudying.service.impl.ManagerServiceImpl;

/**
 * Servlet implementation class ManagerServlet
 */
@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private ManagerService managerService = new ManagerServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManagerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置请求参数的编码格式
				String op = request.getParameter("op");
				if("login".equals(op)) {
					doLogin(request,response);
				}else if ("logout".equals(op)) {
					dologout(request,response);
				}
	}

	private void dologout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//清楚session信息
		HttpSession session = request.getSession();
		session.removeAttribute("managerName");
		session.removeAttribute("managernum");
		session.removeAttribute("managerpsw");
		//重定向
		response.sendRedirect("back/login.jsp");
		
	}

	private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1.调用业务层逻辑。
				String manager_num = request.getParameter("managernum");
				String manager_psw = request.getParameter("managerpsw");
				Map<String, String> managerMap = managerService.login(manager_num, manager_psw);
				
				String managerName = managerMap.get("manager_name");
				String managernum = managerMap.get("manager_num");
				String managerpsw = managerMap.get("manager_psw");
				//System.out.println(managerName+managernum+managerpsw);
				if (managerMap != null) { 
					// 登录成功。
					// 用户信息存储在session会话作用域中
					HttpSession session = request.getSession();
					//session.setAttribute("managerMap", managerMap);
					session.setAttribute("managerName", managerName);
					session.setAttribute("managernum", managernum);
					session.setAttribute("managerpsw", managerpsw);
					
					// 获取复选框组件的值
					String rememberPassword = request.getParameter("rememberPassword");
					// 用户选择记住密码
					if(rememberPassword != null && "rememberPassword".equals(rememberPassword)) {
						// 通过 Servlet 添加 Cookie
						// 用户信息存储在cookie中,发送给HTTP响应头中
						/*多用户操作： name-用户名 value-密码*/
						Cookie cookie  = new Cookie(manager_num, manager_psw);
						response.addCookie(cookie);
						
					}else {
						// 多用户操作,用户没有选择记住密码,删除现有的cookie
						Cookie[] cookies = request.getCookies();
						Cookie cookie = null;
						for (int i = 0; i < cookies.length; i++) {
							cookie = cookies[i];
							if(cookie.getName().equals(manager_num)) {
								cookie.setMaxAge(0);
								response.addCookie(cookie);
							}
						}
					}
				    //  重定向到首页
					response.sendRedirect("back/index.jsp");
	}else {
		// 登录失败 
		// 重定向到登录页面
		response.sendRedirect("back/login.jsp?loginErrorMessage=error");
	}

	}
}
